package com.huluobo.a2204parkdemo.signIn

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.Gson
import com.huluobo.a2204parkdemo.R
import com.huluobo.a2204parkdemo.adapter.SignAdapter
import com.huluobo.a2204parkdemo.databinding.ActivityMonthSignBinding
import com.huluobo.base.bean.MonthSignBean
import com.huluobo.base.mvvm.BaseActivity
import com.huluobo.base.net.Const
import dagger.hilt.android.AndroidEntryPoint
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody

@AndroidEntryPoint
class MonthSignActivity : BaseActivity<ActivityMonthSignBinding, SignInViewModel>() {
    private lateinit var signAdapter: SignAdapter
    private var month: Int = 1
    override fun initData() {
        val map = mutableMapOf<String, Int>()
        map[Const.PARAM_MONTH] = month
        val json = Gson().toJson(map)
        val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
        viewModel.getMonthSign(body)

        viewModel.signMonthSuccess.observe(this) {
            val list = Gson().fromJson(it, MonthSignBean::class.java)
            signAdapter.data.clear()
            signAdapter.data.addAll(list)
            signAdapter.notifyDataSetChanged()
        }
    }

    override fun initView() {
        binding.monthSignHeader.headerTitle.text = "月度考勤明细"
        binding.monthSignHeader.headerBack.setOnClickListener { finish() }

        binding.signMonthRv.layoutManager = LinearLayoutManager(this)
        signAdapter = SignAdapter()
        binding.signMonthRv.adapter = signAdapter

        binding.monthSignTv.text = "${month}月"

        binding.monthSignPreMonth.setOnClickListener {
            if (month > 1) {
                month--
                binding.monthSignTv.text = "${month}月"
                val map = mutableMapOf<String, Int>()
                map[Const.PARAM_MONTH] = month
                val json = Gson().toJson(map)
                val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
                viewModel.getMonthSign(body)
            } else {
                ToastUtils.showLong("已经是一月了")
            }

        }

        binding.monthSignNextMonth.setOnClickListener {
            if (month < 12) {
                month++
                binding.monthSignTv.text = "${month}月"
                val map = mutableMapOf<String, Int>()
                map[Const.PARAM_MONTH] = month
                val json = Gson().toJson(map)
                val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
                viewModel.getMonthSign(body)
            } else {
                ToastUtils.showLong("已经是十二月了")
            }

        }
    }
}